/*
 * Created on Jul 21, 2005
 *
 */
package com.erclab.internal.xpresso.reports.filters;

import java.util.HashMap;

import com.erclab.internal.xpresso.dataAccess.Query;
import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.reports.ReportFilter;
import com.erclab.internal.xpresso.util.MysqlFormatter;
import com.erclab.internal.xpresso.webapp.HTMLHelper;

import org.apache.log4j.*;

/**
 * @author Enrique Rodriguez.
 *
 */
public class MysqlDateFilter extends ReportFilter {
	
	// Create the log for this class
	private static final Logger log = Logger.getLogger(MysqlDateFilter.class);
	
	public void applyFilterToQuery(Query query, HashMap conditions) {
		String[] date1 = (String[])conditions.get("Filter" +  this.columnToFilter);
		String[] conditionType = (String[])conditions.get("Filter" +  this.columnToFilter + "Condition");
		
		if (date1 != null){
			StringBuffer theQuery = new StringBuffer(query.getQuery());
			if (theQuery.indexOf("WHERE") > 0){
				theQuery.append(" AND ");
			} else {
				theQuery.append(" WHERE ");
			}
			theQuery.append(this.columnToFilter);
			query.setQuery(theQuery.toString());
			
			addConditionAndValue(conditionType[0], date1[0], query);
		}
		log.info("Query after filter: " + query.getQuery());
	}
	
	protected void addConditionAndValue(String conditionType, String date1, Query q) {
		if(conditionType.equalsIgnoreCase("Equals")){
			q.setQuery(q.getQuery() + "=?");
			q.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date1));
		/*} else if (conditionType.equalsIgnoreCase("Between")){
			q.setQuery(q.getQuery() + " BETWEEN ? AND ?");
			q.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date1));
			q.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date2));*/
		} else if (conditionType.equalsIgnoreCase("After")){
			q.setQuery(q.getQuery() + " < ?");
			q.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date1));
		} else if (conditionType.equalsIgnoreCase("Before")){
			q.setQuery(q.getQuery() + " > ?");
			q.addValueToQuery(MysqlFormatter.convertDateToMySqlFormat(date1));
		}
		 
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#conditionTypesToHTML()
	 */
	public String conditionTypesToHTML(Report theReport) {
		StringBuffer sb = new StringBuffer();
		sb.append("<SELECT name=\"Filter");
		sb.append(this.columnToFilter + "Condition");
		sb.append("\"");
		sb.append(" id=\"Filter" + this.columnToFilter + "Condition\" class=\"" +  this.style);
		sb.append("\" onChange=\"DateFilterChanged(this)\">");
		
		sb.append("<OPTION value=\"Equals\">Equals</OPTION>");
		//sb.append("<OPTION value=\"Between\">Between</OPTION>");
		sb.append("<OPTION value=\"After\">After</OPTION>");
		sb.append("<OPTION value=\"Before\">Before</OPTION>");
		sb.append("</SELECT>");

		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reports.ReportFilter#filterBoxToHTML()
	 */
	public String filterBoxToHTML(Report theReport) {
		return HTMLHelper.getCalendarBox("Filter" + this.columnToFilter, 
											"Filter" + this.columnToFilter,
											null,
											theReport.getReportName() + "Filters");
	}

}
